﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace Texpro
{
    public partial class FMTR501 : Form
    {

        public FMTR501()
        {
            InitializeComponent();
        }

        private void FMTR502_Load(object sender, EventArgs e)
        {
            lbl_form_title.Text = this.Text;
            load_form();
        }

        private void clear_fields()
        {
            dt_tran_date.Value = PCPUB.m_tran_date;
            lst_tailor.SelectedIndex = -1;
            lst_transport.SelectedIndex = -1;
            txt_total_items.Text = String.Empty;
            txt_total_quantity.Text = String.Empty;
            dgv_detail.Rows.Clear();
            txt_tran_id.Text = String.Empty;
            switch (PCPUB.m_entry_form_mode)
            {
                case "ADDNEW":
                    dt_tran_date.Enabled = false;
                    break;
                case "EDIT":
                    dt_tran_date.Enabled = false;
                    fill_form_data(PCPUB.m_record_date, PCPUB.m_record_id);
                    break;
            }
        }

        private void fill_form_data(string p_date, string p_id)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;// = new OleDbDataReader();
            DateTime strReturn = DateTime.Now;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT CONVERT(DATETIME,[tailoring_ow_date]) tailoring_ow_date,[tailoring_ow_id],[tailor_id],[transport_id]";
            strQuery += " FROM [tis_tailoring_ow]";
            strQuery += " WHERE [tailoring_ow_date] = '" +  PCPUB.m_get_sql_date(p_date) + "'";
            strQuery += " AND [tailoring_ow_id] = '" + p_id + "'";
            //
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            if (sqlRead.Read())
            {
                dt_tran_date.Value = (DateTime)sqlRead["tailoring_ow_date"];
                txt_tran_id.Text = sqlRead["tailoring_ow_id"].ToString();
                lst_tailor.SelectedIndex = lst_tailor.FindStringExact(PCPUB.m_get_tailor_name(sqlRead["tailor_id"].ToString()));
                lst_transport.SelectedIndex = lst_transport.FindStringExact(PCPUB.m_get_transport_name(sqlRead["transport_id"].ToString()));
            }
            //
            strQuery = String.Empty;
            strQuery += " SELECT ";
            strQuery += " [design_id],";
            strQuery += " [size_id],";
            strQuery += " [color_id],";
            strQuery += " [unit_id],";
            strQuery += " [quantity_major],";
            strQuery += " [quantity_minor],";
            strQuery += " [wage_gross],";
            strQuery += " [wage_nett],";
            strQuery += " [amount_gross],";
            strQuery += " [amount_nett]";
            strQuery += " FROM [tis_tailoring_ow_detail]";
            strQuery += " WHERE [tailoring_ow_date] = '" + PCPUB.m_get_sql_date(p_date) + "'";
            strQuery += " AND [tailoring_ow_id] = '" + p_id + "'";
            strQuery += " ORDER BY [tailoring_ow_date],[tailoring_ow_id],[tailoring_ow_detail_id]";
            //
            sqlComm = new OleDbCommand();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            dgv_detail.Rows.Clear();
            int r = -1;
            while (sqlRead.Read())
            {
                dgv_detail.Rows.Add();
                r++;
                dgv_detail["col_design",r].Value = sqlRead["design_id"].ToString();
                dgv_detail["col_size", r].Value = sqlRead["size_id"].ToString();
                dgv_detail["col_color", r].Value = sqlRead["color_id"].ToString();
                dgv_detail["col_unit", r].Value = sqlRead["unit_id"].ToString();
                dgv_detail["col_quantity_major", r].Value = sqlRead["quantity_major"].ToString();
                dgv_detail["col_quantity_minor", r].Value = sqlRead["quantity_minor"].ToString();
                dgv_detail["col_wage_gross", r].Value = sqlRead["wage_gross"].ToString();
                dgv_detail["col_wage_nett", r].Value = sqlRead["wage_nett"].ToString();
                dgv_detail["col_amount_gross", r].Value = sqlRead["amount_gross"].ToString();
                dgv_detail["col_amount_nett", r].Value = sqlRead["amount_nett"].ToString();
            }
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private void load_form()
        {
            PCPUB.m_fill_combo(lst_tailor, "TAILOR");
            PCPUB.m_fill_combo(lst_transport, "TRANSPORT");
            PCPUB.m_fill_combo(col_design, "DESIGN");
            PCPUB.m_fill_combo(col_size, "SIZE");
            PCPUB.m_fill_combo(col_color, "COLOR");
            PCPUB.m_fill_combo(col_unit, "UNIT_FMTR502");
            clear_fields(); 
        }

        private bool save_form()
        {
            try
            {
                String strQuery = String.Empty;
                ArrayList alQueries = new ArrayList();
                OleDbConnection sqlConn = new OleDbConnection();
                OleDbCommand sqlComm = new OleDbCommand();
                OleDbTransaction sqlTran;
                //
                String tailoring_ow_id = String.Empty;
                String tailoring_ow_date = String.Empty;
                String user_id = String.Empty;
                String user_datetime = String.Empty;
                String tailor_id = String.Empty;
                String transport_id = String.Empty;
                //
                String tailoring_ow_detail_id = String.Empty;
                String design_id = String.Empty;
                String size_id = String.Empty;
                String color_id = String.Empty;
                String unit_id = String.Empty;
                String quantity_major = String.Empty;
                String quantity_minor = String.Empty;
                String wage_gross = String.Empty;
                String wage_nett = String.Empty;
                String amount_gross = String.Empty;
                String amount_nett = String.Empty;
                //
                int dgrw = 0;
                int dgrc = 0;
                dgrc = dgv_detail.Rows.Count;
                //
                user_id = "'" + PCPUB.m_user_id + "'";
                user_datetime = "'" + PCPUB.m_get_sql_datetime() + "'";
                tailor_id = "'" + ((m_combo_item)lst_tailor.SelectedItem).combo_code.ToString() + "'";
                transport_id = "'" + ((m_combo_item)lst_transport.SelectedItem).combo_code.ToString() + "'";
                //
                switch (PCPUB.m_entry_form_mode)
                {
                    case "ADDNEW":
                        tailoring_ow_id = "'" + PCPUB.m_get_next_sequence("TAILORING_OW",dt_tran_date) + "'";
                        tailoring_ow_date = "'" + PCPUB.m_get_sql_date(dt_tran_date) + "'";
                        strQuery = String.Empty;
                        strQuery += "INSERT INTO [tis_tailoring_ow] (";
                        strQuery += "[tailoring_ow_date],";
                        strQuery += "[tailoring_ow_id],";
                        strQuery += "[user_id],";
                        strQuery += "[user_datetime],";
                        strQuery += "[tailor_id],";
                        strQuery += "[transport_id]";
                        strQuery += ") VALUES (";
                        strQuery += tailoring_ow_date + ",";
                        strQuery += tailoring_ow_id + ",";
                        strQuery += user_id + ",";
                        strQuery += user_datetime + ",";
                        strQuery += tailor_id + ",";
                        strQuery += transport_id + ");";
                        alQueries.Add(strQuery);
                        break;
                    case "EDIT":
                        tailoring_ow_id = "'" + PCPUB.m_record_id + "'";
                        tailoring_ow_date = "'" + PCPUB.m_get_sql_date(PCPUB.m_record_date) + "'";
                        strQuery = String.Empty;
                        strQuery += " UPDATE [tis_tailoring_ow] SET";
                        strQuery += " [user_id] = " + user_id + ",";
                        strQuery += " [user_datetime] = " + user_datetime + ",";
                        strQuery += " [tailor_id] = " + tailor_id + ",";
                        strQuery += " [transport_id] = " + transport_id;
                        strQuery += " WHERE [tailoring_ow_date] = " + tailoring_ow_date;
                        strQuery += " AND [tailoring_ow_id] = " + tailoring_ow_id + ";";
                        alQueries.Add(strQuery);
                        break;
                }
                //
                strQuery = String.Empty;
                strQuery += "DELETE FROM [tis_tailoring_ow_detail] ";
                strQuery += " WHERE [tailoring_ow_date] = " + tailoring_ow_date;
                strQuery += " AND [tailoring_ow_id] = " + tailoring_ow_id + ";";
                alQueries.Add(strQuery);
                //
                if (dgrc > 1)
                {
                    for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                    {
                        tailoring_ow_detail_id = "'" + PCPUB.m_get_next_sequence("TAILORING_OW_DETAIL") + "'";
                        design_id = "'" + dgv_detail["col_design", dgrw].Value.ToString() + "'";
                        size_id = "'" + dgv_detail["col_size", dgrw].Value.ToString() + "'";
                        color_id = "'" + dgv_detail["col_color", dgrw].Value.ToString() + "'";
                        unit_id = "'" + dgv_detail["col_unit", dgrw].Value.ToString() + "'";
                        quantity_major =  dgv_detail["col_quantity_major", dgrw].Value.ToString();
                        quantity_minor = dgv_detail["col_quantity_minor", dgrw].Value.ToString();
                        wage_gross = dgv_detail["col_wage_gross", dgrw].Value.ToString();
                        wage_nett = dgv_detail["col_wage_nett", dgrw].Value.ToString();
                        amount_gross = dgv_detail["col_amount_gross", dgrw].Value.ToString();
                        amount_nett = dgv_detail["col_amount_nett", dgrw].Value.ToString();
                        //
                        strQuery = String.Empty;
                        strQuery += "INSERT INTO [tis_tailoring_ow_detail] (";
                        strQuery += "[tailoring_ow_date],";
                        strQuery += "[tailoring_ow_id],";
                        strQuery += "[tailoring_ow_detail_id],";
                        strQuery += "[design_id],";
                        strQuery += "[size_id],";
                        strQuery += "[color_id],";
                        strQuery += "[unit_id],";
                        strQuery += "[quantity_major],";
                        strQuery += "[quantity_minor],";
                        strQuery += "[wage_gross],";
                        strQuery += "[wage_nett],";
                        strQuery += "[amount_gross],";
                        strQuery += "[amount_nett]";
                        strQuery += ") VALUES (";
                        strQuery += tailoring_ow_date + ",";
                        strQuery += tailoring_ow_id + ",";
                        strQuery += tailoring_ow_detail_id + ",";
                        strQuery += design_id + ",";
                        strQuery += size_id + ",";
                        strQuery += color_id + ",";
                        strQuery += unit_id + ",";
                        strQuery += quantity_major + ",";
                        strQuery += quantity_minor + ",";
                        strQuery += wage_gross + ",";
                        strQuery += wage_nett + ",";
                        strQuery += amount_gross + ",";
                        strQuery += amount_nett + ");";
                        alQueries.Add(strQuery);
                    }
                }
                //
                sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
                sqlConn.Open();
                sqlTran = sqlConn.BeginTransaction(); ;
                sqlComm.Connection = sqlConn;
                sqlComm.Transaction = sqlTran;
                //
                try
                {
                    for (int i = 0; i < alQueries.Count; i++)
                    {
                        sqlComm.CommandText = alQueries[i].ToString();
                        sqlComm.ExecuteNonQuery();
                    }
                    sqlTran.Commit();
                }
                catch (Exception ex)
                {
                    sqlTran.Rollback();
                    PCMSG.ShowError(ex);
                }
                //
                PCPUB.m_tailoring_ow_id = tailoring_ow_id;
                sqlConn.Close();
                sqlComm.Dispose();
                sqlConn.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
                return false;
            }
        }

        private bool validate_form()
        {
            int  dgrw = 0;
            int  dgrc = 0;
            try
            {
                dgrc = dgv_detail.Rows.Count;
                if (lst_tailor.SelectedIndex == -1)
                {
                    PCMSG.ShowError("Weaver field should not be empty.");
                    lst_tailor.Focus();
                    return false;
                }
                else if (lst_transport.SelectedIndex == -1)
                {
                    PCMSG.ShowError("Transport field should not be empty.");
                    lst_transport.Focus();
                    return false;
                }
                else if (dgrc <= 1)
                {
                    PCMSG.ShowError("Table should not be empty.");
                    dgv_detail["col_design", 0].Selected = true;
                    return false;
                }
                else if (dgrc > 1)
                {
                    for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                    {
                        if (dgv_detail["col_design", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Design field should not be empty.");
                            dgv_detail["col_design", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_size", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Size field should not be empty.");
                            dgv_detail["col_size", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_color", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Color field should not be empty.");
                            dgv_detail["col_color", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_unit", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Unit field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_quantity_major", dgrw].Value == null && dgv_detail["col_quantity_minor", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Quantity field should not be empty.");
                            dgv_detail["col_quantity_major", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_quantity_minor", dgrw].Value != null && long.Parse(dgv_detail["col_quantity_minor", dgrw].Value.ToString()) >= 12)
                        {
                            PCMSG.ShowError("Quantity Minor value should be less than 12.");
                            dgv_detail["col_quantity_minor", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_wage_nett", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Wage field should not be empty.");
                            dgv_detail["col_wage_nett", dgrw].Selected = true;
                            return false;
                        }
                        else if (double.Parse(dgv_detail["col_wage_nett", dgrw].Value.ToString()) <= 0.00)
                        {
                            PCMSG.ShowError("Wage field should not be empty.");
                            dgv_detail["col_wage_nett", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_amount_nett", dgrw].Value == null)
                        {
                            PCMSG.ShowError("Amount field should not be empty.");
                            dgv_detail["col_amount_nett", dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_amount_nett", dgrw].Value != null && double.Parse(dgv_detail["col_amount_nett", dgrw].Value.ToString()) <= 0.00)
                        {
                            PCMSG.ShowError("Amount field should not be empty.");
                            dgv_detail["col_amount_nett", dgrw].Selected = true;
                            return false;
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
                return false;
            }
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (validate_form() == true)
                {
                    if (save_form() == true)
                    {
                        PCMSG.Show("Record saved successfully. Tailoring Outward Id is " + PCPUB.m_tailoring_ow_id.ToString() + ".");
                        PCPUB.m_tailoring_ow_id = String.Empty;
                        clear_fields();
                        lst_tailor.Focus();
                        switch (PCPUB.m_entry_form_mode)
                        {
                            case "ADDNEW":
                                break;
                            case "EDIT":
                                this.Close();
                                break;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
            }
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void update_total_values()
        {
            int total_items = 0;
            long total_quantity_major = 0;
            long total_quantity_minor = 0;
            double total_amount_gross = 0;
            double total_amount_nett = 0;
            //
            string design_id;
            string size_id;
            string unit_id;
            double unit_factor = 1.0;
            long quantity_major = 0;
            long quantity_minor = 0;
            double wage_gross = 0;
            double wage_nett = 0;
            double amount_gross = 0;
            double amount_nett = 0;
            //
            int dgrw = 0;
            int dgrc = 0;
            //
            try
            {
                dgrc = dgv_detail.Rows.Count;
                total_items = dgrc - 1;
                for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                {
                    quantity_major = 0;
                    quantity_minor = 0;
                    amount_gross = 0;
                    amount_nett = 0;
                    wage_gross = 0;
                    wage_nett = 0;
                    design_id = String.Empty;
                    size_id = String.Empty;
                    unit_id = String.Empty;
                    unit_factor = 0;
                    if (dgv_detail["col_design", dgrw].Value != null && dgv_detail["col_size", dgrw].Value != null && dgv_detail["col_unit", dgrw].Value != null)
                    {
                        design_id = dgv_detail["col_design", dgrw].Value.ToString();
                        size_id = dgv_detail["col_size", dgrw].Value.ToString();
                        unit_id = dgv_detail["col_unit", dgrw].Value.ToString();
                        unit_factor = PCDB.m_get_unit_factor(unit_id);
                        wage_gross = PCDB.m_get_tailoring_wage_gross(design_id, size_id, unit_id);
                        wage_nett = PCDB.m_get_tailoring_wage_nett(design_id, size_id, unit_id);
                        dgv_detail["col_wage_gross", dgrw].Value = wage_gross;
                        dgv_detail["col_wage_nett", dgrw].Value = wage_nett;
                        if (dgv_detail["col_quantity_major", dgrw].Value != null)
                            quantity_major = long.Parse(dgv_detail["col_quantity_major", dgrw].Value.ToString());
                        if (dgv_detail["col_quantity_minor", dgrw].Value != null)
                            quantity_minor = long.Parse(dgv_detail["col_quantity_minor", dgrw].Value.ToString());
                        amount_gross = quantity_major * wage_gross;
                        amount_gross += quantity_minor / unit_factor * wage_gross;
                        amount_gross = Math.Round(amount_gross,2);
                        amount_nett = quantity_major * wage_nett;
                        amount_nett += quantity_minor / unit_factor * wage_nett;
                        amount_nett = Math.Round(amount_nett, 2);
                        total_quantity_major += quantity_major;
                        total_quantity_minor += quantity_minor;
                        total_amount_gross += amount_gross;
                        total_amount_nett += amount_nett;
                        dgv_detail["col_amount_nett", dgrw].Value = amount_nett;
                        dgv_detail["col_amount_gross", dgrw].Value = amount_gross;
                   }
                }
                total_quantity_major += (long)Math.Truncate(total_quantity_minor / unit_factor);
                if (unit_factor > 0)
                {
                    total_quantity_minor = total_quantity_minor % (long)unit_factor;
                }
                txt_total_items.Text = total_items.ToString();
                if (total_quantity_major >= 0)
                {
                    txt_total_quantity.Text = total_quantity_major.ToString("#######0") + "." + total_quantity_minor.ToString("00");
                }
                txt_total_wage_nett.Text = total_amount_nett.ToString("#######0.00");
             }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
            }
        }

        private void dgv_detail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 || e.ColumnIndex == 1 || e.ColumnIndex == 2 || e.ColumnIndex == 4 || e.ColumnIndex == 5)
            {
                update_total_values();
            }
        }

    }

}